package com.honglang.module.article.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.honglang.common.tools.Constants;
import com.honglang.common.tools.Pager;
import com.honglang.module.article.dao.BlogArticleDao;
import com.honglang.module.article.entity.BlogArticle;


public class BlogArticleServiceImpl implements BlogArticleService{
	
	private BlogArticleDao blogArticleDao;
	
	
	/**
	 * save the blogArticle
	 * @param blogArticle
	 * @return
	 */
	public BlogArticle save(BlogArticle blogArticle){
		blogArticle = this.blogArticleDao.merge(blogArticle);
		return blogArticle;
		
	}
	
	
	
	
	
	
	/**
	 * get all the blogArticles;
	 * @return
	 */
	public List<BlogArticle> getAll(){
		return this.blogArticleDao.getAll();
	}
	
	
	/**
	 * get the BlogArticle of one page;
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public Pager getPager(int pageSize,int pageNo){
		int rowCount = this.blogArticleDao.getRowCountOfAllEntites();
		Pager pager = new Pager();
		pager.CreatePager(pageSize, pageNo, Constants.pageIndexCountPerPage, rowCount, this.blogArticleDao.getEntitiesPerPage(pageSize, pageNo));
		return pager;
	}
	
	
	/**
	 * get the blogArticle BY id 
	 * @param blogArticleId
	 * @return
	 */
	public BlogArticle getBlogArticleById(Integer blogArticleId){
		
		return this.blogArticleDao.getById(blogArticleId);
	}
	
	
	
	/**
	 * delete blogArticle by id;
	 * @param blogArticleId
	 */
	public void deleteBlogArticle(Integer blogArticleId){
		this.getBlogArticleDao().delete(this.getBlogArticleDao().getById(blogArticleId));
		//this.getBlogArticleDao().delete(blogArticleId);
	}
	
	
	/**
	 * delete blogArticle in List
	 * @param blogArticleList
	 */
	public void deleteBlogArticleList(List<BlogArticle> blogArticleList){
		
		this.getBlogArticleDao().deleteAll(blogArticleList);
	}
	
	
	/**
	 * get blogaArticles by ids (ids formart : "3_12_34")
	 * @param blogArticleIds
	 * @return
	 */
	public List<BlogArticle> getBlogArticleByIds(String blogArticleIds){
		List<Integer> idList = new ArrayList<Integer>();
		for(String id : blogArticleIds.split(Constants.blogArticle_Split)){
			idList.add(Integer.valueOf(id));
		}
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticle.class);
		dc.add(Restrictions.in("id", idList));
		return this.getBlogArticleDao().getByDetachedCriteria(dc);
	}
	
	
	
	/**
	 *  get the blogArticle which date is early then the param
	 * @param date
	 * @return
	 */
	public BlogArticle getPreBlogArticleByDate(Date date){
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticle.class);
		dc.createAlias("article", "article")
		  .add(Restrictions.lt("article.writeDate", date))
		  .addOrder(Order.desc("article.writeDate"));
		List<BlogArticle> blogArticleList = this.getBlogArticleDao().getByDetachedCriteriaInMaxResult(dc,1);
		return blogArticleList.size() > 0? blogArticleList.get(0) : null ;
	}
	
	

	/**
	 *  get the blogArticle which date is later then the param
	 * @param date
	 * @return
	 */
	public BlogArticle getNextBlogArticleByDate(Date date){
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticle.class);
		dc.createAlias("article", "article")
		  .add(Restrictions.gt("article.writeDate", date))
		  .addOrder(Order.desc("article.writeDate"));
		List<BlogArticle> blogArticleList = this.getBlogArticleDao().getByDetachedCriteriaInMaxResult(dc,1);
		return blogArticleList.size() > 0? blogArticleList.get(0) : null ;
	}
	
	
	
	/**
	 * get the article count under a article category;
	 * @param articleCategoryId
	 * @return
	 */
	public Integer getArticleCountInOneCategory(Integer articleCategoryId){
		
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticle.class);
		dc.createAlias("article", "article")
		  .createAlias("article.articleCategories", "category") 	
		  .add(Restrictions.eq("category.id", articleCategoryId));
		return this.getBlogArticleDao().getRowCountByDetachedCriteria(dc);
		
	}
	
	
	/**
	 * get the newest blogArticle;
	 * @param maxResult
	 * @return
	 */
	public List<BlogArticle> getRecentBlogArticle(int maxResult){
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticle.class);
		dc.createAlias("article", "article")
		  .addOrder(Order.desc("article.writeDate"));
		return this.getBlogArticleDao().getByDetachedCriteriaInMaxResult(dc, maxResult);
	}
	
	
	
	
	
	public BlogArticleDao getBlogArticleDao() {
		return blogArticleDao;
	}

	public void setBlogArticleDao(BlogArticleDao blogArticleDao) {
		this.blogArticleDao = blogArticleDao;
	}

	
	
}
